package wstrader.task.home.physical;

import java.sql.ResultSet;
import java.util.Vector;
import wstrader.task.trade.*;
import pc.sql.value.BooleanValue;
import pc.sql.value.DateValue;
import pc.sql.value.IntValue;
import pc.sql.value.StringValue;
import pc.system.*;


public class DailyBasis extends TradeTask
{
  public DailyBasis(Tasks owner)
  {
    super(owner);
  }
  
  public long doTask()
  {
    String table_name = "daily_basis";
    int task_id     = this.getRequest().getIntValue("task_id");
    int RecId = this.getRequest().getIntValue("rec_id");
    long focused_id = this.getRequest().getLongValue("daily_basis");
    String uid = this.getRequest().getStringValue("uid");
     switch (task_id)
	{
      case 1 : // Update
      	if(RecId > 0)
      	{
      		focused_id = doUpdate(table_name,focused_id);
      		getResponse().forwardTo(uid,"home.physical.daily-basis.card.jsp");
      	}
        break;
      case 2 : // Delete
        focused_id = doDelete(table_name,focused_id);
        focused_id = 1;
        break;
      default : //View only
        break;
    }
    this.setFocusedID(focused_id);

    return focused_id;
  }
 
/**
 * @param uid
 * @param string
 */
//inserted by Nara - begin
  public ResultSet fetchDailyBasisList()
  {
  	java.sql.Date DbDt = getRequest().getValueDate("db_date");
  	int OriginId = getRequest().getIntValue("origin");
  	int GradeId = getRequest().getIntValue("grade");
  	String OType = getRequest().getStringValue("otype");
  	int SupplierId = getRequest().getIntValue("supplier");
  	java.sql.Date LsDt = getRequest().getValueDate("ls_date");
  	
  	if (OType.compareTo("")==0) OType="0";
  	String Sql ="Select * from daily_basis where " +
  				"(daily_basis_date = ? OR ?) " +
  		        "and (origin_id = ? OR ?) "+
				"and (grade_id = ? OR ?) " +
				"and (basis_type = ? OR ?) " +
  				"and (seller_id = ? OR ?) " + 
  				"and ((shipment_last_date < ? OR ?) OR (shipment_last_date IS NULL))order by id desc";
    Vector vp = new Vector();

    vp.addElement(new DateValue(DbDt));
    vp.addElement(new BooleanValue(DbDt.compareTo(new java.sql.Date(0)) == 0));

    vp.addElement(new IntValue(OriginId));
    vp.addElement(new BooleanValue(OriginId == 0));

    vp.addElement(new IntValue(GradeId));
    vp.addElement(new BooleanValue(GradeId == 0));

    vp.addElement(new StringValue(OType));
    vp.addElement(new BooleanValue(OType.compareTo("0") == 0));
    
    vp.addElement(new IntValue(SupplierId));
    vp.addElement(new BooleanValue(SupplierId == 0));
  	
	vp.addElement(new DateValue(LsDt));
  	vp.addElement(new BooleanValue(LsDt.compareTo(new java.sql.Date(0)) == 0));
  		
  	return getSQLCommand().executeQuery(Sql,vp);
  }
  
  public String getNew_DailyBasRef()
  {
    String sql =
        "SELECT MAX(ref_number) as ref_number FROM daily_basis ";
    Record rc = new Record(this.getSQLCommand().executeQuery(sql));
    String ref = (!rc.isEmpty()) ? rc.getString("ref_number") : "DB 00-00000";
    rc.close();
    return "DB" + " " + this.getNextRefNumber(ref);
  }   
  //inserted by nara - ends    
 
  
  public ResultSet getDailyBasisLsDt()
  {
  	return getSQLCommand().executeQuery("select distinct shipment_last_date from daily_basis order by shipment_last_date");
  }  
}
